Bootstrap demo

HTML Form Tag Documentation

Table of Contents

1. Introduction to HTML Forms

HTML forms are essential components for collecting user input on web pages. They enable users to submit data to web servers for processing, making them fundamental for interactive web applications.

2. Basic Form Structure

The basic structure of an HTML form consists of the <form> tag containing various input elements:

<form action="/submit" method="post">
  <!-- Form elements go here -->
  <input type="submit" value="Submit">
</form>

3. Form Attributes

Attribute Description Values Example
action Specifies where to send the form data when submitted URL or server endpoint action="/submit-data"
method Defines the HTTP method used to send form data get or post method="post"
enctype Specifies how form data should be encoded application/x-www-form-urlencoded
multipart/form-data
text/plain
enctype="multipart/form-data"
target Specifies where to display the response _blank, _self, _parent, _top target="_blank"
autocomplete Controls browser autocomplete functionality on or off autocomplete="off"
novalidate Prevents browser validation novalidate novalidate
name Specifies the name for JavaScript reference Any string name="contactForm"

4. Form Elements

Core Form Elements

5. Input Types

Text Inputs

Date and Time Inputs

Selection Inputs

Button Inputs

6. Form Validation

HTML5 Validation Attributes

Custom Validation Methods

7. Form Submission Methods

Traditional Form Submission

AJAX Form Submission

Fetch API Submission

8. Best Practices

Accessibility

Security

Usability

Performance

9. Examples

Basic Contact Form

<form action="/contact" method="post">
  <label for="name">Name:</label>
  <input type="text" id="name" name="name" required>
  
  <label for="email">Email:</label>
  <input type="email" id="email" name="email" required>
  
  <label for="message">Message:</label>
  <textarea id="message" name="message" required></textarea>
  
  <button type="submit">Send Message</button>
</form>

Registration Form with Validation

<form action="/register" method="post" novalidate>
  <fieldset>
    <legend>Personal Information</legend>
    
    <label for="username">Username:</label>
    <input type="text" id="username" name="username" 
           pattern="[a-zA-Z0-9_]+" minlength="3" maxlength="20" required>
    
    <label for="password">Password:</label>
    <input type="password" id="password" name="password" 
           minlength="8" required>
    
    <label for="age">Age:</label>
    <input type="number" id="age" name="age" min="18" max="120" required>
  </fieldset>
  
  <fieldset>
    <legend>Preferences</legend>
    
    <label for="newsletter">Subscribe to newsletter:</label>
    <input type="checkbox" id="newsletter" name="newsletter">
    
    <label>Notification frequency:</label>
    <select name="frequency">
      <option value="daily">Daily</option>
      <option value="weekly">Weekly</option>
      <option value="monthly">Monthly</option>
    </select>
  </fieldset>
  
  <button type="submit">Register</button>
  <button type="reset">Clear Form</button>
</form>

File Upload Form

<form action="/upload" method="post" enctype="multipart/form-data">
  <label for="file">Select file to upload:</label>
  <input type="file" id="file" name="file" accept=".jpg,.png,.pdf">
  
  <label for="description">File description:</label>
  <input type="text" id="description" name="description">
  
  <button type="submit">Upload File</button>
</form>